package com.vrv.controller.analysis;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vrv.entity.analysis.ChartInfo;
import com.vrv.entity.analysis.ChartPort;
import com.vrv.entity.analysis.PublishChart;
import com.vrv.entity.analysis.TabelData;
import com.vrv.entity.analysis.ViewInfo;
import com.vrv.utils.Constans;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.MyGson;
import com.vrv.utils.StringUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/analysis/chart")
public class ChartController {

    @RequestMapping(value = "/chartList")
    public String chartList(ModelMap model) {
        String target = XMLRead.HND_IP + "chartList";
        System.out.println(target);
        String resultData = HttpClientUtil.getInstance().get(target);
        // String resultData =
        // "[{\"chartname\":\"图表1\",\"charttype\":\"2\",\"chartarea\":\"3\",\"xname\":\"education\",\"yname\":\"enum\",\"viewname\":\"视图1\",\"viewrows\":\"2\",\"viewcols\":\"3\",\"chartdata\":\"select gnum, gender from default.student\",\"viewid\":\"5a9wwe0-b716-45bc-a2f1-8e7427\",\"chartid\":\"5a93f5e0-b716-45bc-a2f1-8d72182e7427\"}]";
        List<ChartInfo> list = MyGson.toList(resultData);
        model.put("list", list);
        return "/analysis/chart_list";
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd(ModelMap model) {
        String createURL = XMLRead.HND_IP + "createChart";
        System.out.println(createURL);
        String createData = HttpClientUtil.getInstance().get(createURL);
        List<TabelData> tabelList = MyGson.toList(createData);
        model.put("tabelList", tabelList);
        return "/analysis/chart_edit";
    }

    @RequestMapping(value = "/datalist", produces = Constans.PRODUCES)
    @ResponseBody
    public String datalist(String querySql) throws UnsupportedEncodingException {
        String target = XMLRead.HND_IP + "datalist";
        System.out.println(target);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("querysql", querySql);
        String encode = URLEncoder.encode(JsonUtil.MapToJson(map), "utf-8");
        String resultData = HttpClientUtil.getInstance().get(target + "?querysql=" + encode);
        return resultData;
    }

    @RequestMapping(value = "/delChart", produces = Constans.PRODUCES)
    @ResponseBody
    public String delChart(String id, String viewid) {
        String target = XMLRead.HND_IP + "delChart?id=" + id + "&viewid=" + viewid;
        System.out.println(target);
        String jsonString = HttpClientUtil.getInstance().get(target);
        return jsonString;
    }

    @RequestMapping(value = "/ylChart")
    public String chartList(ModelMap model, String id) {
        String target = XMLRead.HND_IP + "ylChart?id=" + id;
        System.out.println(target);
        String resultData = HttpClientUtil.getInstance().get(target);
        ChartPort chartPort = (ChartPort) MyGson.fromJson(resultData, ChartPort.class);
        model.put("chartPort", chartPort);
        return "/analysis/ylchart";
    }

    @RequestMapping(value = "/datapreview")
    public String datapreview(ModelMap model, PublishChart publishChart)
            throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("querysql", publishChart.getQuerysql());
        map.put("chartname", publishChart.getChartname());
        map.put("xzhou", publishChart.getXzhou());
        map.put("yzhou", publishChart.getYzhou());
        map.put("charttype", publishChart.getCharttype());
        String target = XMLRead.HND_IP + "datapreview";
        System.out.println(target);
        String encode = URLEncoder.encode(JsonUtil.MapToJson(map), "utf-8");
        String resultData = HttpClientUtil.getInstance().get(target + "?data=" + encode);
        ChartPort chartPort = (ChartPort) MyGson.fromJson(resultData, ChartPort.class);
        model.put("chartPort", chartPort);
        return "/analysis/ylchart";
    }

    @RequestMapping(value = "/toPublish")
    public String toPublish(ModelMap model, PublishChart publishChart) {
        String target = XMLRead.HND_IP + "viewlist";
        String resultData = HttpClientUtil.getInstance().get(target);

        Gson gson = new Gson();
        List<ViewInfo> veiwlist = gson.fromJson(resultData, new TypeToken<List<ViewInfo>>() {
        }.getType());
        for (int i = 0; null != veiwlist && i < veiwlist.size(); i++) {
            if ("1".equals(veiwlist.get(i).getViewflag())) {
                veiwlist.remove(i);
            }
        }
        model.put("veiwlist", veiwlist);
        model.put("publishChart", publishChart);
        return "/analysis/chart_publish";
    }

    @RequestMapping(value = "/publishchart", produces = Constans.PRODUCES)
    @ResponseBody
    public String publishchart(PublishChart publishChart) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("querysql", publishChart.getQuerysql());
        map.put("chartname", publishChart.getChartname());
        map.put("xzhou", publishChart.getXzhou());
        map.put("yzhou", publishChart.getYzhou());
        map.put("charttype", publishChart.getCharttype());
        map.put("areaid", publishChart.getAreaid());
        map.put("viewid", publishChart.getViewid());

        String target = XMLRead.HND_IP + "publishchart";
        System.out.println(target);
        String encode = URLEncoder.encode(JsonUtil.MapToJson(map), "utf-8");
        String resultData = HttpClientUtil.getInstance().get(target + "?data=" + encode);
        return resultData;
    }

    @RequestMapping(value = "/getViewByID", produces = Constans.PRODUCES)
    @ResponseBody
    public String publishchart(String viewid) throws UnsupportedEncodingException {
        String target = XMLRead.HND_IP + "getViewByID/?viewid=" + viewid;
        System.out.println(target);
        String resultData = HttpClientUtil.getInstance().get(target);

        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = MyGson.toList(resultData);
        if (!StringUtil.listEmpty(list)) {
            for (Map<String, Object> map2 : list) {
                map.put(map2.get("area") + "", map2);
            }
        }
        return JsonUtil.MapToJson(map);
    }
}
